package com.slixils.demo.validator;

import com.slixils.demo.utils.ValidatorUtil;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author: JinChongLi
 * @Description:
 * @Date: Created in 14:57 2022/4/7
 * @Modified By:
 */
@Configuration
public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {
    private boolean required=false;
    @Override
    public void initialize(IsMobile constraintAnnotation) {
       required = constraintAnnotation.required();
    }
    public  String valid;

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (required) {
            valid=s;
            return ValidatorUtil.isMobile(s);
        } else {
            if (StringUtils.isEmpty(s)) {
                return true;
            } else {
                return ValidatorUtil.isMobile(s);
            }
        }
    }
}
